<template>
  <section>
    <el-row style="display: flex;align-items: center;">
      <el-button size="mini" @click="addSportMethod" style="float: right" type="primary">新建运动方式</el-button>
    </el-row>
    <el-row>
      <!--sportId、name、detail(选)、code(选)、data(选)、method(选)、documentId(选)、effect(选)-->
      <el-table :data="sportMethodList" stripe v-loading="loading">
        <el-table-column type="index" label="序号"  width="50px"></el-table-column>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="图例：">
                <upload-img v-on:get="getImgUrl($event,props.row)"></upload-img>
                <el-input size="mini" v-model="props.row.data.img"></el-input>
                <img :src="props.row.data.img"/>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="名称">
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.name"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="code" label="编码" >
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.code"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.code}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="effect" label="作用" >
          <template slot-scope="scope">
            <el-input v-show="scope.row.edit" size="mini" v-model="scope.row.effect"></el-input>
            <span  v-show="!scope.row.edit">{{scope.row.effect}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="documentId" label="文件Id">
          <template slot-scope="scope">
          </template>
        </el-table-column>
        <el-table-column label="操作" width="160" fixed="right">
          <template slot-scope="scope">
            <el-button :type="scope.row.edit?'success':'primary'" @click='confirm(scope.row)' size="mini" icon="edit">{{scope.row.edit?'完成':'编辑'}}</el-button>
            <el-button @click='cancelBtn(scope.row)' size="mini" v-if="scope.row.edit">取消</el-button>
            <el-button @click='del(scope)' size="mini" v-if="!scope.row.edit" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </section>
</template>

<script>
  import {mapGetters, mapActions, mapMutations} from 'vuex'
  import Vue from 'vue'//后续添加属性，需要vue联动
  import errHandler from '@/assets/errHandler'
  import uploadImg from  '@/common/uploadImg'
  export default {
    name: "sportMethod",
    components: {uploadImg},
    props:['sport'],
    data() {
      return {
        loading:false,
        sportMethodList:null
      }
    },
    created(){
      console.log(this.sport)
      this.getSportMethodList(this.sport.id)
    },
    computed:{
    },
    watch: {
      sport(nv,ov){
        if(nv){
          console.log(nv)
          this.getSportMethodList(nv.id)
        }
      }
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      addSportMethod(){
        //新建一个空的food，表格页面+1栏
        this.fetch({name:"createSportMethod",data:{name:'',sportId:this.sport.id}})
          .then(res=>{
            console.log(res)
            res.edit = true
            this.sportMethodList.unshift(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      getSportMethodList(sportId){
        this.fetch({name:"getSportMethodList",data:{sportId}})
          .then(res=>{
            console.log(res)
            this.loading=false
            this.sportMethodList=res.content
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      confirm(row){
        if(row.edit === undefined){
          Vue.set(row,'edit',true)//后续添加属性，需要vue联动
          return
        }
        if(row.edit){
          this.loading=true
          this.updateSportMethod(row)
        }else {
          row.edit=true
        }
      },
      cancelBtn(row){//取消按钮
        if(row.edit === undefined){
          Vue.set(row,'edit',false)//后续添加属性，需要vue联动
        }else {
          row.edit =false
        }
      },
      del(scope){//删除按钮
        this.loading=true
        this.$confirm('确认删除？')
          .then(()=> {
            this.fetch({name:"delSportMethod",data:{id:scope.row.id}})
              .then(res=>{
                this.loading=false
                this.sportMethodList.splice(scope.$index,1)
              })
              .catch(err=>{
                console.log(err)
                errHandler.handle(this,err)
              })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
            this.loading = false
          })
      },
      updateSportMethod(row){
        this.fetch({name:"updateSportMethod",data:{...row}})
          .then(res=>{
            this.loading=false
            console.log(res)
            row.edit=false
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
